using System;
using System.Reflection;
using UnityEditor;

namespace IQIGame.Onigao.Framework.Editors
{
    [CustomEditor(typeof(ObjectPoolManager))]
    class ObjectPoolManagerInspector : Editor
    {
        private SerializedProperty m_DefaultAutoReleaseInterval;
        private SerializedProperty m_DefaultSpecificResCapacity;
        private SerializedProperty m_DefaultTotalCapacity;

        private void OnEnable()
        {
            m_DefaultAutoReleaseInterval = serializedObject.FindProperty("m_DefaultAutoReleaseInterval");
            m_DefaultSpecificResCapacity = serializedObject.FindProperty("m_DefaultSpecificResCapacity");
            m_DefaultTotalCapacity = serializedObject.FindProperty("m_DefaultTotalCapacity");
        }

        public override void OnInspectorGUI()
        {
            serializedObject.Update();
            ObjectPoolManager component = (ObjectPoolManager)target;

            EditorGUILayout.LabelField("默认检查（释放）间隔（秒）");
            float floatValue = EditorGUILayout.FloatField("Default Release Interval", m_DefaultAutoReleaseInterval.floatValue);
            if (floatValue != m_DefaultAutoReleaseInterval.floatValue)
            {
                if (EditorApplication.isPlaying)
                {
                    SetPrivateFieldValue("m_DefaultAutoReleaseInterval", floatValue);
                }
                else
                {
                    m_DefaultAutoReleaseInterval.floatValue = floatValue;
                }
            }
            EditorGUILayout.Space();
            EditorGUILayout.LabelField("默认同预设资源实例容量");
            int intValue = EditorGUILayout.IntField("Default SpecificRes Capacity", m_DefaultSpecificResCapacity.intValue);
            if (intValue != m_DefaultSpecificResCapacity.intValue)
            {
                if (EditorApplication.isPlaying)
                {
                    SetPrivateFieldValue("m_DefaultSpecificResCapacity", intValue);
                }
                else
                {
                    m_DefaultSpecificResCapacity.intValue = intValue;
                }
            }
            EditorGUILayout.Space();
            EditorGUILayout.LabelField("默认资源实例总容量");
            intValue = EditorGUILayout.IntField("Default Total Capacity", m_DefaultTotalCapacity.intValue);
            if (intValue != m_DefaultTotalCapacity.intValue)
            {
                if (EditorApplication.isPlaying)
                {
                    SetPrivateFieldValue("m_DefaultTotalCapacity", intValue);
                }
                else
                {
                    m_DefaultTotalCapacity.intValue = intValue;
                }
            }
            EditorGUILayout.HelpBox("以上字段仅代表默认值，创建对象池时如果不设置，则取该值，否则取设置的值。", MessageType.Info);

            serializedObject.ApplyModifiedProperties();
        }

        private void SetPrivateFieldValue<T>(string fieldName, T fieldValue)
        {
            ObjectPoolManager component = (ObjectPoolManager)target;
            Type type = component.GetType();
            FieldInfo fieldInfo = type.GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic);
            fieldInfo.SetValue(component, fieldValue);
        }
    }
}
